import 'dart:math';
import 'dart:convert' as convert;

import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class bookrack_class extends StatefulWidget {
  bookrack_class(
      {Key key,
      this.user_bookrack_class,
      this.NumChange,
      this.user_bookrack_class_num,
      this.setList})
      : super(key: key);
  List user_bookrack_class;
  Function setList;
  // Void set(int e, String name);
  final ValueChanged<int> NumChange;

  int user_bookrack_class_num;
  @override
  State<bookrack_class> createState() => _bookrack_classState();
}

class _bookrack_classState extends State<bookrack_class> {
  TextEditingController textFController =
      new TextEditingController(); //声明controller

  // ignore: unused_element
  _newboolrack() async {
    Response response;
    var dio = Dio();
    dio.options.responseType = ResponseType.plain;
    response = await dio.get(
        'http://119.91.231.198:3033/newBookrack?bookrack_name=' +
            textFController.text);
    var res = convert.jsonDecode(response.toString());
    await widget.setList();
    print(widget.user_bookrack_class);
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  bool state = false;
  bool settingState = false;
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: [
          Container(
            height: 60,
            padding: EdgeInsets.only(left: 15),
            child: Row(
              children: [
                Text('书架分组(' +
                    widget.user_bookrack_class.length.toString() +
                    ')'),
                Expanded(child: SizedBox()),
                Row(
                  children: [
                    Offstage(
                      offstage: settingState,
                      child: Container(
                          margin: EdgeInsets.only(right: 6),
                          child: InkWell(
                            onTap: () {
                              print('添加');
                              showDialog(
                                  context: context,
                                  builder: (context) {
                                    return AlertDialog(
                                      content: Container(
                                        height: 75,
                                        child: Column(children: [
                                          Text('新建书架'),
                                          TextField(
                                            controller: textFController,
                                            keyboardType: TextInputType.number,

                                            // onChanged: _textFieldChanged,
                                            autofocus: false,
                                          ),
                                        ]),
                                      ),
                                      actions: <Widget>[
                                        FlatButton(
                                          onPressed: () {
                                            // newBookrack
                                            _newboolrack();
                                            setState(() {
                                              widget.user_bookrack_class.add({
                                                "bookrack_id": 8,
                                                "bookrack_name":
                                                    textFController.text,
                                                "bookrack_list": 0
                                              });
                                            });

                                            Navigator.of(context).pop();
                                          },
                                          child: Text(
                                            '新建',
                                            style: TextStyle(
                                                color: Color.fromRGBO(
                                                    80, 200, 255, 1)),
                                          ),
                                          textColor: Colors.red,
                                        ),
                                        FlatButton(
                                          onPressed: () {
                                            setState(() {
                                              // _history.clear();
                                              print('取消');
                                              // Application.sp.remove("search_history");
                                              // 发送删除用户历史记录
                                            });
                                            Navigator.of(context).pop();
                                          },
                                          child: Text('取消'),
                                          textColor: Colors.black54,
                                        ),
                                      ],
                                    );
                                  });
                            },
                            child: Container(
                              padding: EdgeInsets.all(8),
                              child: Icon(
                                Icons.add,
                                color: Color.fromRGBO(50, 170, 255, 1),
                              ),
                            ),
                          )),
                    ),
                    Container(
                        margin: EdgeInsets.only(right: 10),
                        child: InkWell(
                          onTap: () {
                            setState(() {
                              settingState
                                  ? settingState = false
                                  : settingState = true;
                            });
                          },
                          child: Container(
                            padding: EdgeInsets.all(8),
                            child: settingState
                                ? Text(
                                    '完成',
                                    style: TextStyle(
                                        color: Color.fromRGBO(50, 170, 255, 1)),
                                  )
                                : Icon(
                                    Icons.create,
                                    color: Color.fromRGBO(50, 170, 255, 1),
                                  ),
                          ),
                        )),
                  ],
                ),
              ],
            ),
          ),
          Wrap(
            spacing: 10,
            children: widget.user_bookrack_class
                .asMap()
                .keys
                .map(((v) => InkWell(
                      onTap: () {
                        setState(() {
                          settingState
                              ? {}
                              // ignore: unnecessary_statements
                              : {
                                  widget.NumChange(v),
                                  widget.user_bookrack_class_num = v
                                };
                        });
                      },
                      child: Container(
                        padding: EdgeInsets.only(right: 20),
                        height: 60,
                        margin: EdgeInsets.only(top: 2, bottom: 2),
                        // color: Colors.red,
                        child: Row(
                          children: [
                            Offstage(
                              offstage: !settingState ||
                                  widget.user_bookrack_class[v]
                                          ['bookrack_name'] ==
                                      '全部',
                              child: Icon(Icons.delete_forever),
                            ),
                            Container(
                              width: MediaQuery.of(context).size.width / 2,
                              margin: EdgeInsets.only(top: 8, left: 12),
                              child: Column(
                                children: [
                                  Container(
                                      alignment: Alignment.centerLeft,
                                      child: Text(
                                        widget.user_bookrack_class[v]
                                                ['bookrack_name']
                                            .toString(),
                                        style: TextStyle(
                                          color:
                                              widget.user_bookrack_class_num ==
                                                      v
                                                  ? Color.fromRGBO(
                                                      50, 170, 255, 1)
                                                  : Colors.black87,
                                        ),
                                      )),
                                  Container(
                                      alignment: Alignment.centerLeft,
                                      child: Text(
                                        '共' +
                                            widget.user_bookrack_class[v]
                                                    ['bookrack_list']
                                                .toString() +
                                            '本',
                                        style: TextStyle(
                                            fontSize: 12,
                                            color: Colors.black54),
                                      ))
                                ],
                              ),
                            ),
                            Expanded(child: SizedBox()),
                            Offstage(
                              offstage: settingState,
                              child: Icon(
                                Icons.done,
                                color: widget.user_bookrack_class_num == v
                                    ? Color.fromRGBO(50, 170, 255, 1)
                                    : Color.fromRGBO(50, 170, 255, 0),
                              ),
                            ),
                          ],
                        ),
                      ),
                    )))
                .toList(),
          )
        ],
      ),
    );
  }
}
